﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Reflection;
using System.Dynamic;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

using System.Web.Caching;

namespace TestTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {            
            InitializeComponent();
        }

        private void btnbegin_Click(object sender, EventArgs e)
        {
            Init();
        }
        public void Init()
        {
            this.panel1.Controls.Clear();
            IEnumerable<string> strs = System.IO.File.ReadLines(this.txtapi.Text);
            int count1 = 0; //行数
            int count2 = 0; //列数
            int count = 0;
            var title = "";
            bool begin = false;
            Label label = new Label();
            Button btn = new Button();
            string param = "";
            foreach (string str in strs)
            {
                if (str.Contains("/*") && str.Contains("*/"))
                {
                    if (count % 3 != 0)
                    {
                        count1++;
                    }
                    //count1++;
                    label = new Label();
                    label.Text = str.Replace("/*", "").Replace("*/", "").Trim();
                    label.Font = new Font("", 16);
                    label.ForeColor = Color.Blue;
                    label.Size = new System.Drawing.Size(200, 30);
                    label.Location = new System.Drawing.Point(5, count1 * 35);
                    this.panel1.Controls.Add(label);
                    count1++;
                    count2 = 0;
                    count = 0;

                }
                if (str.Contains("#region"))
                {
                    count++;
                    if (count2 % 3 == 0)
                    {
                        count2 = 0;
                    }

                    begin = true;
                    title = (str.Replace("#region", "").Trim());
                    btn = new Button();
                    btn.Text = title;
                    btn.Size = new System.Drawing.Size(130, 30);
                    btn.Location = new System.Drawing.Point(count2 * 140 + 10, count1 * 35);
                    btn.Click += new EventHandler(btn_Click);
                    this.panel1.Controls.Add(btn);

                    if (count2 % 3 == 2)
                    {
                        count1++;
                    }
                    count2++;
                }
                if (str.Contains("hs.Add"))
                {
                    param = "";
                    var str0 = str.Split('"');
                    
                    param += "?act=" + str0[1];
                    param += "&token=" + Common.GetConfig("Token") + "{extra}";
                }
                if (begin && str.Contains("JobRequest"))
                {
                    var str1 = str.Split('"');
                    if (str1.Length >= 2)
                    {
                        string val = "";
                        if (str.ToLower().Contains("int") || str.ToLower().Contains("float") || str.ToLower().Contains("double"))
                        {
                            val = "1";
                        }
                        if (str1[1] == "source")
                        {
                            val = "2";
                        }
                        if (str.ToLower().Contains("string"))
                        {
                            val = "";
                        }
                        if (str.ToLower().Contains("bool"))
                        {
                            val = "true";
                        }
                        if (str.ToLower().Contains("date") || str.ToLower().Contains("time"))
                        {
                            val = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        }
                        if (str.ToLower().Contains("username"))
                        {
                            val = "test";
                        }
                        if (str.ToLower().Contains("mobile"))
                        {
                            val = "13523595900";
                        }
                        if (str.ToLower().Contains("address"))
                        {
                            val = "河南省郑州市";
                        }
                        if (str.ToLower().Contains("name") || str.ToLower().Contains("title"))
                        {
                            val = "测试";
                        }
                        if (str.ToLower().Contains("realname"))
                        {
                            val = "张三";
                        }

                        if (str.ToLower().Contains("pwd"))
                        {
                            val = "123456";
                        }
                        param += "&" + str1[1] + "=" + val;
                    }

                }
                if (str.Contains("#endregion"))
                {
                    begin = false;
                    btn.Tag = param;

                }
            }
        }
        void btn_Click(object sender, EventArgs e)
        {
            Control.ControlCollection colls = this.panel2.Controls;
            for (int i = colls.Count-1; i >=0 ;i-- )
            {
                Control coll = colls[i];
                if (coll.GetType().ToString().Contains("TextBox"))
                {
                    TextBox textbox = (TextBox)coll;

                    if (textbox.Name != "txtbox1")
                    {
                        colls.Remove(coll);
                    }
                }
                if (coll.GetType().ToString().Contains("Label"))
                {
                    colls.Remove(coll);
                }
            }


            Button btn = (Button)sender;
            
            this.textBox1.Text = "";
            string[] paras = btn.Tag.ToString().Trim('?').Split('&');
            int count = 0;
            Label label;
            TextBox text;
            long timestamp = Common.GetTimestamp();
            string extra = "";
            foreach(string str in paras){
                var v=str.Split('=');
                if (v[0] == "act") { extra += "&sign="+ Common.MakeSign(v[1], timestamp); extra += "&timestamp=" + timestamp; continue; }
                if (v[0] == "token") { continue; }
                label = new Label();
                label.Text = v[0];
                label.Size = new System.Drawing.Size(100, 20);
                label.Location = new System.Drawing.Point(0, 78+count * 35);
                this.panel2.Controls.Add(label);

                text = new TextBox();
                text.Text = v[1];
                text.Size = new System.Drawing.Size(130, 30);
                text.Location = new System.Drawing.Point(100, 76+count * 35);
                text.KeyUp += new KeyEventHandler(text_KeyUp);
                this.panel2.Controls.Add(text);

                count++;
            }

            this.txtbox1.Text = this.txtdomain.Text + btn.Tag.ToString().Replace("{extra}", extra);
        }

        void text_KeyUp(object sender, KeyEventArgs e)
        {
            string url = "";
            string[] urls = this.txtbox1.Text.Split('?');
            string[] paras = urls[1].Split('&');
            foreach (string para in paras)
            {
                if (para.Split('=')[0] == "act")
                {
                    url = urls[0] +"?"+ para;
                }
                if (para.Split('=')[0] == "sign")
                {
                    url += "&" + para;
                }
                if (para.Split('=')[0] == "token")
                {
                    url +=  "&" + para;
                }
                if (para.Split('=')[0] == "timestamp")
                {
                    url += "&" + para;
                }
            }

            Control.ControlCollection colls = this.panel2.Controls;
            for (int i = 0; i < colls.Count; i++)
            {
                Control coll = colls[i];
                if (i == 0 || i == 1) { continue; }
                if (coll.GetType().ToString().Contains("Label"))
                {
                    Label label = (Label)coll;
                    url += "&" + label.Text;
                }
                if (coll.GetType().ToString().Contains("TextBox"))
                {
                    TextBox textbox = (TextBox)coll;
                    url += "=" + textbox.Text;
                }
            }

            this.txtbox1.Text = url;
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
            this.txtapi.Text = this.openFileDialog1.FileName;
            Common.saveAPI(this.txtapi.Text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.txtdomain.Text = Common.GetConfig("Domain");
            this.txtapi.Text = Common.GetConfig("API");
            Init();
            try
            {
                WeiXin wx = new WeiXin();
                long timestamp=Common.GetTimestamp();
                string json = wx.HttpGet(this.txtdomain.Text + "?act=Login&sign=" + Common.MakeSign("Login", timestamp) + "&username=" + Common.GetConfig("UserName") + "&mobile=" + Common.GetConfig("UserName") + "&userpwd=" + Common.GetConfig("UserPwd") + "&timestamp=" + timestamp, "");
                dynamic obj= wx.ConvertJson(json);
                Common.saveUserToken(obj.data.Token);
                Init();
            }
            catch (Exception ex)
            {
            }
        }

        private void linkset_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form2 fm2 = new Form2();
            fm2.ShowDialog();
            Init();
        }

        

        private void txtdomain_TextChanged(object sender, EventArgs e)
        {
            Common.saveDomain(this.txtdomain.Text);
        }

        private void txtapi_TextChanged(object sender, EventArgs e)
        {
            Common.saveAPI(this.txtapi.Text);
        }

        private void btnsend_Click(object sender, EventArgs e)
        {
            try
            {
                WeiXin wx = new WeiXin();
                string json = wx.HttpGet(this.txtbox1.Text, "");
                dynamic obj = wx.ConvertJson(json);
                if (obj.state == true)
                {
                    string[] ss = Regex.Split(json, "data\":");
                    string str = obj.msg + "\r\n\r\n" + ss[1].Replace("{", "").Replace("}", "").Replace("\"", "").Replace(",", "\r\n");
                    this.textBox1.Text = str;
                }
                else
                {
                    this.textBox1.Text = obj.msg;
                }
                if (this.textBox1.Lines.Count()>42)
                {
                    this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                }
            }
            catch (Exception ex)
            {
                this.textBox1.Text = ex.Message;
            }
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            this.panel1.Focus();
        }

    }
}
